package cn.ibizlab.odoo.webcrm;

import java.util.List;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import com.fasterxml.jackson.databind.ObjectMapper;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.cloud.openfeign.FeignClientsConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScans;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import cn.ibizlab.odoo.util.web.SearchContextHandlerMethodArgumentResolver;
import cn.ibizlab.odoo.util.serialize.DomainSerializerProvider;

@Slf4j
@SpringBootApplication(exclude = {

})
@Import({
    FeignClientsConfiguration.class
})
@ComponentScans({
        @ComponentScan(basePackages = {"cn.ibizlab.odoo.util", "cn.ibizlab.odoo.webcrm"})
})
@EnableDiscoveryClient
@Configuration
@EnableFeignClients
@EnableConfigurationProperties(cn.ibizlab.odoo.webcrm.WebCrmApplication.odoo_crmClientProperties.class)
public class WebCrmApplication extends WebMvcConfigurerAdapter{

    public static void main(String[] args) {
        SpringApplicationBuilder builder=new SpringApplicationBuilder(WebCrmApplication.class);
        builder.run(args);
    }

    @Bean
    public MappingJackson2HttpMessageConverter mappingJacksonHttpMessageConverter(ObjectMapper objectMapper) {
    	objectMapper.setSerializerProvider(new DomainSerializerProvider()) ;
    	final MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter(objectMapper);
        return converter;
    }

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        super.addArgumentResolvers(argumentResolvers);
        argumentResolvers.add(new SearchContextHandlerMethodArgumentResolver());
    }

    @ConfigurationProperties(prefix = "app.service.odoo.crm")
    @Data
    public class odoo_crmClientProperties {

    	private String tokenUrl ;

    	private String clientId ;

    	private String clientSecret ;

    	private String serviceUrl ;

    	private String serviceId ;

    }
}
